package com.yimixian.app.ui;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;

/**
 * Created by zhilian-2 on 2015/11/11.
 */
public class AroundPoiLinearlayout extends LinearLayout{

    public interface ShowAnimationStatus{

        void isShowAnimation(boolean isUp);
    }

    private float scrollDistance = 10;

    private ListView childListView;

    private boolean isInitChildListView;

    private boolean isUp;//true为上 false为下

    private boolean isListViewToScrollTop;

    private boolean isIntercept;

    private float startY = 0;

    ShowAnimationStatus showAnimationStatus;

    public AroundPoiLinearlayout(Context context) {
        super(context);
//        initView();
    }

    public AroundPoiLinearlayout(Context context, AttributeSet attrs) {
        super(context, attrs);
//        initView();
    }

    public AroundPoiLinearlayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        initView();
    }
//
//    private void initView() {
//        if (isInitChildListView) return;
//        if (getChildCount() > 0) {
//            isInitChildListView = true;
//            childListView = (ListView) getChildAt(0);
//            childListView.setOnScrollListener(new AbsListView.OnScrollListener() {
//                public void onScrollStateChanged(AbsListView view, int scrollState) {
//                    switch (scrollState) {
//                        // 当不滚动时
//                        case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
//                            // 判断滚动到底部
//                            if (childListView.getLastVisiblePosition() == (childListView.getCount() - 1)) {
//                            }
//                            // 判断滚动到顶部
//                            if (childListView.getFirstVisiblePosition() == 0) {
//                                isListViewToScrollTop = true;
//                            }else{
//                                isListViewToScrollTop = false;
//                            }
//                            break;
//                    }
//                }
//                @Override
//                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//                }
//            });
//        }
//    }
//
//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        initView();
//        final int action = MotionEventCompat.getActionMasked(ev);
//        switch (action) {
//            case MotionEvent.ACTION_DOWN:
//                startY = ev.getY();
//                isIntercept = false;
//                break;
//            case MotionEvent.ACTION_MOVE:
//                if (startY - ev.getY() > scrollDistance && !isUp && showAnimationStatus != null){
//                    isIntercept = true;
//                    isUp = true;
//                    showAnimationStatus.isShowAnimation(true);
//                }else if (startY - ev.getY() < -scrollDistance && isListViewToScrollTop &&
//                        showAnimationStatus != null && isUp){
//                    isIntercept = true;
//                    isUp = false;
//                    showAnimationStatus.isShowAnimation(false);
//                }else
//                    isIntercept = false;
//                break;
//            case MotionEvent.ACTION_UP:
//                break;
//            default:
//                break;
//        }
//        return isIntercept;
//    }

    public void setShowAnimationStatus(ShowAnimationStatus showAnimationStatus) {
        this.showAnimationStatus = showAnimationStatus;
    }

    public void setDownAnimation(){
        if (isUp && showAnimationStatus != null){
            isIntercept = true;
            isUp = false;
            showAnimationStatus.isShowAnimation(false);
        }
    }
}
